package io.dbw.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class MathUtil {
	public static final String ADD = "ADD";
	public static final String SUBTRACT = "SUBTRACT";
	public static final String MULTIPLY = "MULTIPLY";
	public static final String DIVIDE = "DIVIDE";

	/**
	 * 方法名:calcFloatValue      
	 * 方法功能描述:float类型的加减乘除
	 * @param: type: 加：ADD   减：  SUBTRACT  乘：MULTIPLY 除：DIVIDE      
	*/
	public static Float calcFloatValue(Float t1, Float t2, String type) {
		if(t1==null){
			t1=0f;
		}
		if(t2==null||t2==0){
			return t1;
		}
		BigDecimal a = new BigDecimal(String.valueOf(t1));
		BigDecimal b = new BigDecimal(String.valueOf(t2));
		float retValue = 0f;
		switch (type) {
		case ADD:
			retValue = a.add(b).floatValue();
			break;
		case SUBTRACT:
			retValue = a.subtract(b).floatValue();
			break;
		case MULTIPLY:
			retValue = a.multiply(b).floatValue();
			break;
		case DIVIDE:
			retValue = a.divide(b,2,BigDecimal.ROUND_HALF_UP).floatValue();
			break;
		}
		return retValue;
	}

	/**
	 * 方法名:calcFloatValue      
	 * 方法功能描述:float类型的加减乘除
	 * @param: type: 加：ADD   减：  SUBTRACT  乘：MULTIPLY 除：DIVIDE      
	 */
	public static Long  calcFloatValue(BigDecimal a,String type) {
		BigDecimal b = new BigDecimal(100);
		if(null ==a){
			 return null;
		}
		long retValue = 0L;
		switch (type) {
			case ADD:
				retValue = a.add(b).longValue() ;
				break;
			case SUBTRACT:
				retValue = a.subtract(b).longValue() ;
				break;
			case MULTIPLY:
				retValue = a.multiply(b).longValue() ;
				break;
			case DIVIDE:
				retValue = a.divide(b,2,BigDecimal.ROUND_HALF_UP).longValue() ;
				break;
		}
		return retValue;
	}

	public static BigDecimal  calcBigDecimalValue(Long f,String type) {
		BigDecimal t3 = new BigDecimal(0);
		if(null ==f){
			return null;
		}
		BigDecimal a = new BigDecimal(f);
		BigDecimal b = new BigDecimal(100);
		switch (type) {
			case ADD:
				t3 = a.add(b) ;
				break;
			case SUBTRACT:
				t3 = a.subtract(b) ;
				break;
			case MULTIPLY:
				t3 = a.multiply(b) ;
				break;
			case DIVIDE:
				t3 = a.divide(b,2,BigDecimal.ROUND_HALF_UP) ;
				break;
		}
		return t3;
	}

	public static BigDecimal  calcBigDecimalValue(BigDecimal f1,Integer f2,String type) {
		BigDecimal t3 = new BigDecimal(0);
		if(null ==f1){
			return null;
		}
		BigDecimal b = new BigDecimal(f2);
		switch (type) {
			case ADD:
				t3 = f1.add(b) ;
				break;
			case SUBTRACT:
				t3 = f1.subtract(b) ;
				break;
			case MULTIPLY:
				t3 = f1.multiply(b) ;
				break;
			case DIVIDE:
				t3 = f1.divide(b,2,BigDecimal.ROUND_HALF_UP) ;
				break;
		}
		return t3;
	}

	public static Long float2Long100(Float t) {
		BigDecimal a = new BigDecimal(String.valueOf(t));
		BigDecimal b = new BigDecimal("100");
		return a.multiply(b).longValue();
	}
	/**
	 * 格式成2位小数
	 * @param f
	 * @return
	 */
	public static Float format2d(Float f) {
		return format(f,2);
	}
	/**
	 * 格式成num位小数
	 * @param f
	 * @return
	 */
	public static Float format(Float f,int num) {
		return f==null?0f:new BigDecimal(f).setScale(num,BigDecimal.ROUND_HALF_UP).floatValue();
	}
	public static BigDecimal format(String f,int num) {
		return StringUtils.isBlank(f)?null:new BigDecimal(f).setScale(num,BigDecimal.ROUND_HALF_UP) ;
	}
	/**
	 * 格式成1位小数
	 * @param f
	 * @return
	 */
	public static Float format(Float f) {
		return format(f,1);
	}
	/**
	 * 将double格式化为指定小数位的String，不足小数位用0补全
	 * @param v     需要格式化的数字
	 * @param scale 小数点后保留几位
	 * @return
	 */
	public static String roundByScale(Double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException(
					"The scale must   be   a   positive   integer   or   zero");
		}
		if(null ==v){
			v=0d;
		}
		if(scale == 0){
			return new DecimalFormat("0").format(v);
		}
		String formatStr = "0.";
		for(int i=0;i<scale;i++){
			formatStr = formatStr + "0";
		}
		return new DecimalFormat(formatStr).format(v);
	}

	public static Float toFloat(Float f) {
		if(null==f){
			return 0.0f;
		}
		return f;
	}


}